package com.miya.demo.support.rabbitmq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@Slf4j
public class StockMqListener1 {

	@RabbitListener(bindings = @QueueBinding(
			value = @Queue(value = "stockMarketQueue1", durable = "true", exclusive = "false", autoDelete = "false"),
			exchange = @Exchange(value = "stockMarketExchange", type = ExchangeTypes.FANOUT)))
	public void process(Message message, Channel channel) throws Exception {
		long deliveryTag = message.getMessageProperties().getDeliveryTag();
		try {
			String messageStr = new String(message.getBody(), StandardCharsets.UTF_8);
			log.info("stockMarketQueue1 mq 通知，消息体：【{}】", messageStr);

			channel.basicAck(deliveryTag, false);
		} catch (Exception ex) {
			log.error("StockMqListener1 error", ex);
			channel.basicAck(deliveryTag, false);
			throw ex;
		}
	}
}
